{% extends 'home.html' %}
{% block content-main %}
    <style>
        .demo-login-container {
            width: 320px;
            margin: 21px auto 0;
        }

        .demo-login-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
    <form class="layui-form" id="login_form">

        <div class="demo-login-container">
            <div class="layui-form-item">
                {% csrf_token %}
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                           lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="" lay-verify="required" placeholder="密   码"
                           lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-vercode"></i>
                            </div>
                            <input type="text" name="captcha" value="" lay-verify="required" placeholder="验证码"
                                   lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="{% url 'user:verify_code' %}"
                                 onclick="this.src='{% url 'user:verify_code' %}'+'?t='+ new Date().getTime();">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <input type="button" lay-submit lay-filter="demo-login" class="layui-btn layui-btn-fluid" value="登录">
            </div>
            <div class="layui-form-item demo-login-other text-center">
                <a href="{% url 'home' %}">返回首页</a>
                或 <a href="{% url 'user:register' %}">注册帐号</a>
            </div>
        </div>
    </form>

    <script>
        layui.use(function () {
            let form = layui.form;
            {#var layer = layui.layer;#}
            // 提交事件
            form.on('submit(demo-login)', function (data) {
                let formData = new FormData;
                let field = data.field; // 获取表单字段值
                // 遍历field中的数据{key:value}
                $.each(field, function (key, value) {
                    // 将数据添加到formdata对象传给Ajax
                    formData.append(key, value)
                });
                // 通过ajax上传数据显示填写结果，仅作演示用
                $.ajax({
                    url: '',
                    type: 'post',
                    data: formData,
                    // 设置下面两个属性，告知浏览器不要对数据进行处理
                    // 否则可能会转为字符串格式或二进制个数，后端拿去内容有误
                    contentType: false, // 不要设置headers
                    processData: false, // 不要处理数据
                    success: (response) => {
                        console.log(response)
                        if (response.code === 200) {
                            alert(response.msg)
                            window.location.href='{% url 'home' %}'
                        } else {
                            alert(response.errors)
                            window.location.href='{% url 'user:login' %}'
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        });
    </script>
{% endblock %}